IF OBJECT_ID('ssf.gbl_enable_semaphore') IS NOT NULL
	DROP PROCEDURE ssf.gbl_enable_semaphore
GO

CREATE PROCEDURE ssf.gbl_enable_semaphore
	@semaphore_name NVARCHAR(128)
	, @connection_count INT
	, @semaphore_description NVARCHAR(2048) = NULL
AS
/*
<object_header>

<object_summary>
	<purpose><![CDATA[

	]]></purpose>
</object_summary>

</object_header>
*/

SET NOCOUNT ON

-- ============
/*<code_comment><![CDATA[declare dynamic sql variables]]></code_comment>*/
DECLARE	@proc_error INT
			, @sql_error INT
			, @error_message NVARCHAR(2048)
			, @error_line INT
			, @row_count BIGINT
			, @nsql NVARCHAR(MAX)
			, @crlf CHAR(2) = CHAR(13) + CHAR(10)
			, @quote CHAR(1) = CHAR(39)
			, @tab CHAR(1) = CHAR(9)
-- ============

-- ============
/*<code_comment><![CDATA[check parameters]]></code_comment>*/
IF NULLIF(@semaphore_name, N'') IS NULL
OR @connection_count IS NULL
BEGIN
	RAISERROR('A required parameter is blank or null.', 16, 1)
	RETURN 1
END
-- ============

-- ============
/*<code_comment><![CDATA[begin: insert semaphore if it does not exist]]></code_comment>*/
IF ssf.gbl_exists_variable(@semaphore_name) = 0
BEGIN
	-- ============
	/*<code_comment><![CDATA[define semaphore]]></code_comment>*/
	EXECUTE @proc_error = ssf.gbl_insert_variable
		@variable_name = @semaphore_name
		, @variable_description = @semaphore_description
		, @variable_value = @connection_count

	IF @proc_error <> 0
	BEGIN
		RAISERROR('The call to the procedure <ssf.gbl_insert_variable> failed.', 16, 1)
		RETURN @proc_error
	END
	-- ============
	
	-- ============
	/*<code_comment><![CDATA[set connection count]]></code_comment>*/
	EXECUTE @proc_error = ssf.gbl_set_semaphore_connection_count
		@semaphore_name = @semaphore_name
		, @connection_count = @connection_count
	
	IF @proc_error <> 0
	BEGIN
		RAISERROR('The call to the procedure <ssf.gbl_set_semaphore_connection_count> failed.', 16, 1)
		RETURN @proc_error
	END
	-- ============
	
	-- ============
	/*<code_comment><![CDATA[exit]]></code_comment>*/
	RETURN @@ERROR
	-- ============
END
/*<code_comment><![CDATA[end: insert semaphore if it does not exist]]></code_comment>*/
-- ============

-- ============
/*<code_comment><![CDATA[set semaphore value]]></code_comment>*/
EXECUTE @proc_error = ssf.gbl_update_variable_value
	@variable_name = @semaphore_name
	, @variable_value = @connection_count

IF @proc_error <> 0
BEGIN
	RAISERROR('The call to the procedure <ssf.gbl_update_variable_value> failed.', 16, 1)
	RETURN @proc_error
END
-- ============

-- ============
/*<code_comment><![CDATA[set connection count]]></code_comment>*/
EXECUTE @proc_error = ssf.gbl_set_semaphore_connection_count
	@semaphore_name = @semaphore_name
	, @connection_count = @connection_count

IF @proc_error <> 0
BEGIN
	RAISERROR('The call to the procedure <ssf.gbl_set_semaphore_connection_count> failed.', 16, 1)
	RETURN @proc_error
END
-- ============

RETURN @@ERROR
GO
